﻿package appAdater 
{
	import flash.display.*;
	import flash.events.*;
	import flash.system.ApplicationDomain;
	import flash.utils.*;
	
	public class SpicyParticle extends Sprite implements IAppAdater
	{
		private var spicy:*;
		private var domain:ApplicationDomain;
		private var index:int;
		public function SpicyParticle(domain:ApplicationDomain,stage:Stage,index:int)
		{
			
			this.domain=domain;
			this.index=index;
			
			
		}
		public function start():void{
			setTimeout(t,300);
		}
		
		private function t():void{
			var EvoSpicyParticleEngine:Class=domain.getDefinition("evospicyparticleengine.EvoSpicyParticleEngine") as Class;
			this.spicy =new EvoSpicyParticleEngine(stage, index, false, true);
			this.spicy.on3DFailed.add(this.on3DFailed);
			this.spicy.onGPUInfo.add(this.onGPUInfo);
			this.spicy.onEngineReady.add(this.onEngineReady);
		}
		
		private function on3DFailed() : void
		{
			//trace("3D Failed");
			return;
		}// end function
		
		private function onGPUInfo(info:String) : void
		{
			//trace("GPUInfo: " + info);
			return;
		}// end function
		
		private function resize(e:Event):void{
			var stage3D:Stage3D=stage.stage3Ds[index];
			if(this.stage.displayState == StageDisplayState.FULL_SCREEN){
				stage3D.x=0;
				stage3D.y=0;
				return;
			}
			var _aspect:Number = (Application.WIDTH+1) / Application.HEIGHT;
			var _zNear:Number = 0.1;
			var _zFar:Number = 10000;
			var _fov:Number = 60 * Math.PI / 180;
			spicy.perspective(_fov, _aspect, _zNear, _zFar);
			
			
			try{
				
				stage3D.x=Application.instance.iphoneContainer.x+Application.padding.left;
				stage3D.y=Application.instance.iphoneContainer.y+Application.padding.top;
				stage3D.context3D.configureBackBuffer(Application.WIDTH+1,Application.HEIGHT,0);
			}catch(e:Error){
				
			}
		}
		
		
		private function onEngineReady() : void
		{
			if(isStop)return;
			
			var stage3D:Stage3D=stage.stage3Ds[index];
			//stage3D.context3D.dispose();
			
			
			stage.addEventListener(Event.RESIZE,resize);
			
			resize(null);
			
			var RendererLinear:Class=domain.getDefinition("evospicyparticleengine.render.RendererLinear") as Class;
			var ValueExplosionSphere:Class=domain.getDefinition("evospicyparticleengine.buffer.value.ValueExplosionSphere") as Class;
			var TextureParticleAlphaKill:Class=domain.getDefinition("evospicyparticleengine.texture.TextureParticleAlphaKill") as Class;
			var ColorGradient:Class=domain.getDefinition("evospicyparticleengine.color.ColorGradient") as Class;
			var ProgramLinear:Class=domain.getDefinition("evospicyparticleengine.program.ProgramLinear") as Class;
			var EaseProgram:Class=domain.getDefinition("evospicyparticleengine.ease.EaseProgram") as Class;
			var StartPoint3D:Class=domain.getDefinition("evospicyparticleengine.buffer.StartPoint3D") as Class;
			
			
			this.spicy.setDepthTest(true);
			this.spicy.setTextureParticle(new TextureParticleAlphaKill(128));
			this.spicy.setBlendMode(BlendMode.NORMAL);
			this.spicy.zFar = 30000;
			this.spicy.fov = 90;
			this.spicy.addParticles(139808, new ValueExplosionSphere(new ColorGradient([3916287, 8669211, 16761343, 3916287]), 100, 100, 100, 2500, 2500, 2500, 16, true, 2));
			var _loc_2:* = new RendererLinear(new ProgramLinear(EaseProgram.LINEAR, false, true), 8e-005, 128, 1973015);
			
			var _loc_1:* = null;
			_loc_1 = new RendererLinear(new ProgramLinear(EaseProgram.LINEAR, false, true), 8e-005, 128, 1973015);
			this.spicy.setRenderer(_loc_2);
			_loc_1.startPoints[0].y = -500;
			
			_loc_1.addStartPoint(new StartPoint3D(0, 500, 0));
			this.addEventListener(Event.ENTER_FRAME, this.run);
			return;
		}// end function
		
		private var isStop:Boolean=false;
		public function pause():void{
			isStop=!isStop;
			if(isStop){
				this.removeEventListener(Event.ENTER_FRAME, this.run);
				stage.removeEventListener(Event.RESIZE,resize);
			}else{
				resize(null);
				this.addEventListener(Event.ENTER_FRAME, this.run);
				stage.addEventListener(Event.RESIZE,resize);
			}
		}
		
		public function unload():void{
			var stage3D:Stage3D=stage.stage3Ds[index];
			stage3D.x=0;
			stage3D.y=0;
			stage3D.context3D.dispose();//没得玩了;
		}
		
		private function run(event:Event) : void
		{
			if(isStop)return;
			var _loc_2:* = getTimer();
			var _loc_3:* = _loc_2 * 0.0001;
			var _loc_4:Number = 1000;
			this.spicy.camera.position.x = _loc_4 * Math.sin(_loc_3);
			this.spicy.camera.position.z = _loc_4 * Math.cos(_loc_3);
			this.spicy.camera.position.y = 0;
			this.spicy.camera.lookAtPoint(0, 0, 0);
			this.spicy.camera.rotationZ = _loc_2 * 0.01;
			this.spicy.render();
			return;
		}// end function
		
	}
}
